package testCase.DataDevelopService;

import com.sun.org.glassfish.gmbal.Description;
import io.restassured.response.Response;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import utils.PropertiesUtils;
import utils.RequestUtils;

import java.util.HashMap;
import java.util.Map;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;

public class GetQuerysql2 {

    String connectId;//scheme+'://'+host+url
    String scheme;//请求协议
    String host;
    String baseUrl;
    String url;
    String configPath;
    Map<String,String> queryParams = new HashMap<String, String>();
//    Map<String,String> queryParams2 = new HashMap<String, String>();
    Map<String,String> queryParamsValue = new HashMap<String, String>();
    String method ;

    @BeforeTest(groups = {"getQuerysql"})
    void getParams() throws Exception {

        System.out.println("test");
        configPath = PropertiesUtils.class.getClassLoader().getResource("config.properties").getPath();//获取配置文件路径
        String packageName = GetQuerysql2.class.getPackage().getName();
        String[] packageNameArr = packageName.split("\\.");
        int length = packageNameArr.length;
        scheme = PropertiesUtils.GetValueByKey(configPath,packageNameArr[length-1]+".scheme");
        host = PropertiesUtils.GetValueByKey(configPath,packageNameArr[length-1]+".host");

        RequestUtils.getHeaderData("ddsCase.xlsx","getQuerysql");
        baseUrl = RequestUtils.baseUrl;
        url = scheme+"://"+host+baseUrl;
    }



    @Test(dependsOnMethods = {"getQuerysql2"},groups = {"getQuerysql"})
    @Description("获取sql语句")
    void getQuerysql1() throws Exception {

        queryParamsValue = RequestUtils.Login(1);
        System.out.println("GETSQL:"+queryParamsValue);
        queryParams.put("schemaName",queryParamsValue.get("schemaName"));
        queryParams.put("tableName",queryParamsValue.get("tableName"));
        connectId = queryParamsValue.get("connectId");

        //given()：一次网络请求所需要的条件都写在这里，头信息、query参数
        // when()：触发条件
        //then()：断言
        Response response =
                 given().log().all()
                        .pathParam("connectId",connectId)
                        .queryParams(queryParams)//查询参数
                .when()
                         .get(url)
                .then()
                         .log().all()
                         .statusCode(200)
                         .body("response",containsString("select"))
                .extract()
                         .response();
       // System.out.println(response.asString());
//        //通过字符串方法查找是否包含具体的验证内容
//        String bodyAsString = response.getBody().asString();
//        Assert.assertEquals(bodyAsString.contains("select") , true , "报文正文select");
    }



    @Test(groups = {"getQuerysql"})
    @Description("模式名、表名为中文时获取sql语句")
    void getQuerysql2() throws Exception {
        queryParamsValue.clear();
        queryParams.clear();
        queryParamsValue = RequestUtils.Login(2);
        System.out.println("GETSQL:"+queryParamsValue);

        queryParams.put("schemaName",queryParamsValue.get("schemaName"));
        queryParams.put("tableName",queryParamsValue.get("tableName"));
        connectId = queryParamsValue.get("connectId");

//        queryParams2.put("schemaName","中文");
//        queryParams2.put("tableName","中文");
//        connectId = "5f1655ad21392400010ac041";
        Response response = given().log().all()
                .when()
                .pathParam("connectId",connectId)
                .queryParams(queryParams)//查询参数
                .get(url)
                .then()
                .extract()
                .response();
//        System.out.println(response.asString());
        //通过字符串方法查找是否包含具体的验证内容
//        String bodyAsString = response.getBody().asString();
//        Assert.assertEquals(bodyAsString.contains("select") , true , "报文正文select");
    }

//    @Test
//    @Description("模式名、表名带有特殊字符时获取sql语句")
//    void getQuerysql3(){
//        queryParams2.put("schemaName","中文");
//        queryParams2.put("tableName","中文");
//        connectId = "5f1655ad21392400010ac041";
//        Response response = given()
//                .when()
//                .pathParam("connectId",connectId)
//                .queryParams(queryParams2)//查询参数
//                .get("http://backend.comm.sit.wingconn.com/DataDevelopmentMgtService/dataSources/{connectId}/querySql")
//                .then()
//                .extract()
//                .response();
////        System.out.println(response.asString());
//        //通过字符串方法查找是否包含具体的验证内容
////        String bodyAsString = response.getBody().asString();
////        Assert.assertEquals(bodyAsString.contains("select") , true , "报文正文select");
//    }

//public static void main(String [] args){
//
//        //new GetQuerysql().getParams();
//
//}
}
